package com.takepoints21.mobile;

import java.util.Collection;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.takepoints21.mobile.zxing.camera.CameraManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.SurfaceHolder;

public class VerifyCodeQR extends BaseActivity implements SurfaceHolder.Callback {
	
	private static final String TAG = VerifyCodeQR.class.getSimpleName();
	
	private CameraManager cameraManager;
	//private ViewfinderView viewfinderView;
	//private CaptureActivityHandler handler;
	private Result savedResultToShow;
	private Collection<BarcodeFormat> decodeFormats;
	private String characterSet;
	private boolean hasSurface;
	
	/*public ViewfinderView getViewfinderView() {
		return viewfinderView;
	}
	public Handler getHandler() {
		return handler;
	}*/
	public CameraManager getCameraManager() {
		return cameraManager;
	}
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verifycodeqr);
        manageMenu(0);
	}
	
	@Override
	protected void onResume(){
	    super.onResume();
	    cameraManager = new CameraManager(getApplication());
/*
	    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
	    viewfinderView.setCameraManager(cameraManager);
	    
	    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.camera_preview);
	    SurfaceHolder surfaceHolder = surfaceView.getHolder();
	    if(hasSurface){
	    	initCamera(surfaceHolder);
	    } else {
	    	surfaceHolder.addCallback(this);
	        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	    }
	    
	    Intent intent = getIntent();
	    if(intent != null){
	        String action = intent.getAction();
	        String dataString = intent.getDataString();
	        if(Intents.Scan.ACTION.equals(action)){
	        	cameraManager.setManualFramingRect(200, 200);

	            String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
	            if(customPromptMessage != null){
	            	popup("Result : " + customPromptMessage);
	            } else{
	            	popup("No Result : Action = " + action);
	            }
	        }
	    } else{
	    	popup("No Scan");
	    }
*/
	    //IntentIntegrator integrator = new IntentIntegrator(VerifyCodeQR.this);
	    //integrator.initiateScan();
        /*try{
		    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
			intent.putExtra("SCAN_WIDTH", 800);
			intent.putExtra("SCAN_HEIGHT", 200);
			intent.putExtra("RESULT_DISPLAY_DURATION_MS", 3000L);
			intent.putExtra("PROMPT_MESSAGE", "Custom prompt to scan a product");
			startActivityForResult(intent, IntentIntegrator.REQUEST_CODE);
	    } catch(Exception e){
	    	alert("Error : " + e.getMessage());
	    }*/
	}
	
	@Override
	protected void onPause(){
		if(cameraManager != null){
			cameraManager.closeDriver();
		}
	}
	
	/*protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
		switch(requestCode) { 
	        case IntentIntegrator.REQUEST_CODE: {
	            if (resultCode != RESULT_CANCELED){ 
	                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
	                if (scanResult != null){
	                    String upc = scanResult.getContents();
	                    popup("Result : " + upc);
	                } 
	            } 
	            break; 
	        }
	    }
	}*/
/*
	private void initCamera(SurfaceHolder surfaceHolder){
	    try {
			cameraManager.openDriver(surfaceHolder);
			if(handler == null){
				handler = new CaptureActivityHandler(this, decodeFormats, characterSet, cameraManager);
			}
			decodeOrStoreSavedBitmap(null, null);
	    } catch(IOException ioe){
	    	Log.w(TAG, ioe);
	    } catch(RuntimeException e){
	    	Log.w(TAG, "Unexpected error initializing camera", e);
	    }
	}
	
	private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result){
	    // Bitmap isn't used yet -- will be used soon
	    if(handler == null){
	      savedResultToShow = result;
	    } else{
	      if(result != null){
	    	  savedResultToShow = result;
	      }
	      if(savedResultToShow != null){
	    	  Message message = Message.obtain(handler, R.id.decode_succeeded, savedResultToShow);
	    	  handler.sendMessage(message);
	      }
	      savedResultToShow = null;
	    }
	}
	
	public void handleDecode(Result rawResult, Bitmap barcode){
		if(barcode != null){
	    	drawResultPoints(barcode, rawResult);
	    }
	}
	
	private void drawResultPoints(Bitmap barcode, Result rawResult) {
	    ResultPoint[] points = rawResult.getResultPoints();
	    if (points != null && points.length > 0) {
	      Canvas canvas = new Canvas(barcode);
	      Paint paint = new Paint();
	      paint.setColor(getResources().getColor(R.color.result_image_border));
	      paint.setStrokeWidth(3.0f);
	      paint.setStyle(Paint.Style.STROKE);
	      Rect border = new Rect(2, 2, barcode.getWidth() - 2, barcode.getHeight() - 2);
	      canvas.drawRect(border, paint);

	      paint.setColor(getResources().getColor(R.color.result_points));
	      if (points.length == 2) {
	        paint.setStrokeWidth(4.0f);
	        drawLine(canvas, paint, points[0], points[1]);
	      } else if (points.length == 4 &&
	                 (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
	                  rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
	        // Hacky special case -- draw two lines, for the barcode and metadata
	        drawLine(canvas, paint, points[0], points[1]);
	        drawLine(canvas, paint, points[2], points[3]);
	      } else {
	        paint.setStrokeWidth(10.0f);
	        for (ResultPoint point : points) {
	          canvas.drawPoint(point.getX(), point.getY(), paint);
	        }
	      }
	    }
	}
	
	private static void drawLine(Canvas canvas, Paint paint, ResultPoint a, ResultPoint b) {
		canvas.drawLine(a.getX(), a.getY(), b.getX(), b.getY(), paint);
	}
	
	public void drawViewfinder() {
		viewfinderView.drawViewfinder();
	}
*/
	@Override
	public void onBackPressed(){
		Intent detailInt = new Intent(this, Takepoints.class);
		startActivityForResult(detailInt, 0);
		return;
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3){
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder){
		/*if(holder == null){
			Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
	    }
	    if(!hasSurface){
	    	hasSurface = true;
	    	initCamera(holder);
	    }*/
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder){
		hasSurface = false;
	}
}
